import grails.converters.*

class DefaultController {


    
    static roleAccess =  [
            [action:["*"], roles:["admin"]],
            [action:["index","show","list","login","loginProcess"], roles:["*"]],
	]
	    
    def index = { 
	def t = []
	def q = []
	t << Grole.findByName('admin')       	 
	q << Grole.findByName('default')       	 


	def n = new News();
	def newsStories = n.getNewsForRoles(session?.user?.getRoleList())

	if(session?.user) {
		if(session.user.hasRole("foo")) { 
			println "has foo role"
		}
		if(session.user.hasRole("admin")) { 
			println "has admin role"
		}
		if(session.user.hasPermission("foo")) { 
			println "user can foo"
		}
		if(session.user.hasPermission("read")) { 
			println "user can read"
		}
		if(session.user.hasPermission("edit")) { 
			println "user can edit"
		}
	}

	def temp = grailsApplication.config.permissions

	[news:newsStories]
    }
    
    def update = {
        
	println System.currentTimeMillis()   
        def info = [:]
        def xml = new XmlSlurper().parse(new File("/tmp/a.xml"));
        xml.controllers.controller.each { c->
                def cname = c.@name
                info["${cname}"] = [:]
                c.action.each { ac->
                        def i = ac.@name
                        info["${cname}"]["${i}"] = []
                        ac.role.each { r->
				def roleName = r.@name
                                info["${cname}"]["${i}"] << "${roleName}"
                        }
                }
        }
        
	grailsApplication.config.permissions = info
	println System.currentTimeMillis()   
    }
}
